package com.wan37.logic.team.handler;

import com.wan37.common.uuid.UUIDUtil;
import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.listener.EventDispatcher;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserAddTeamEvent;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.team.TeamManager;
import com.wan37.logic.team.cache.TeamCache;
import com.wan37.logic.team.domain.vo.TeamVO;
import com.wan37.logic.team.util.TeamRouteUtil;
import com.wan37.protobuf.constant.CommonTeam;
import com.wan37.protobuf.message.client2server.game.GameTeam;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author : luoyong
 * @date : 2020-07-30 15:50
 **/
public class CGameTeamHandler extends MsgHandler {
    private TeamRouteUtil teamRouteUtil;
    private TeamCache teamCache;
    private UserServiceCache userServiceCache;
    private TeamManager teamManager;
    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameTeamHandler(MessageVO messageVO) {
        super(messageVO);
        this.teamRouteUtil = SpringContextHolder.getBean("teamRouteUtil");
        this.teamCache = SpringContextHolder.getBean("teamCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.teamManager = SpringContextHolder.getBean("teamManager");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameTeam.TeamMsg teamMsg = (GameTeam.TeamMsg) messageVO.getMessage();
        int code = teamMsg.getCode();
        switch (code){
            case CommonTeam.CREATE:
                if (userServiceCache.getUserServiceMap().get(messageVO.getUserId()).getTeamName() != null){
                    teamRouteUtil.sendTeamResponse(CommonTeam.EXIST_TEAM_IN, new ArrayList<>(), messageVO.getUserId());
                    break;
                }
                if (teamCache.getTeamInMap().containsKey(teamMsg.getTeamName())){
                    teamRouteUtil.sendTeamResponse(CommonTeam.EXIST, new ArrayList<>(), messageVO.getUserId());
                    break;
                }
                TeamVO teamVO = new TeamVO();
                teamVO.setCaptain(messageVO.getUserId());
                teamVO.setTeamId(UUIDUtil.getUUID());
                teamVO.setTeamName(teamMsg.getTeamName());
                List<String> userIdList = new CopyOnWriteArrayList<>();
                userIdList.add(messageVO.getUserId());
                teamVO.setUserIdList(userIdList);
                TeamCache.TeamIn teamIn = teamCache.new TeamIn();
                teamIn.setTeamVO(teamVO);
                teamCache.getTeamInMap().put(teamVO.getTeamName(), teamIn);
                teamRouteUtil.sendTeamResponse(CommonTeam.CREATE, teamVO, messageVO.getUserId());
                //用户缓存中添加所在队伍名称
                userServiceCache.getUserServiceMap().get(messageVO.getUserId()).setTeamName(teamVO.getTeamName());

                break;
            case CommonTeam.JOIN:
                if (userServiceCache.getUserServiceMap().get(messageVO.getUserId()).getTeamName() != null){
                    teamRouteUtil.sendTeamResponse(CommonTeam.EXIST_TEAM_IN, new ArrayList<>(), messageVO.getUserId());
                    break;
                }
                if (!teamCache.getTeamInMap().containsKey(teamMsg.getTeamName())){
                    teamRouteUtil.sendTeamResponse(CommonTeam.NOT_EXIST, new ArrayList<>(), messageVO.getUserId());
                    break;
                }
                TeamVO teamVO1 = teamCache.getTeamInMap().get(teamMsg.getTeamName()).getTeamVO();
                teamVO1.getUserIdList().add(messageVO.getUserId());
                for (String userId : teamVO1.getUserIdList()){
                    teamRouteUtil.sendTeamResponse(CommonTeam.UPDATE, teamVO1, userId);
                }
                //用户缓存中添加所在队伍名称
                userServiceCache.getUserServiceMap().get(messageVO.getUserId()).setTeamName(teamVO1.getTeamName());
                EventDispatcher.getInstance().fireEvent(new UserAddTeamEvent(EventType.ADD_TEAM, messageVO.getUserId()));
                break;
            case CommonTeam.OUT:
                teamManager.out(messageVO.getUserId());
                break;
            case CommonTeam.GET_ALL:
                List<TeamVO> teamVOList = new ArrayList<>();
                for (TeamCache.TeamIn teamIn1 : teamCache.getTeamInMap().values()){
                    teamVOList.add(teamIn1.getTeamVO());
                }
                teamRouteUtil.sendTeamResponse(CommonTeam.GET_ALL, teamVOList, messageVO.getUserId());
                break;
            default:
                break;
        }
    }
}
